﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BulldogCMS.Data
{
    public class Database : IDatabase
    {
        private IStorage _storage = null;
        private Cache.ICacheProvider _cacheProvider = null;
        private IDatabaseDefinitionProvider _dbDefinitionProvider = null;
        private DataContext.IDataContext _dataContext = null;
        private Guid _userID;
        private ContentFolder _content = null;
        private FilesTree.FilesFolder _files = null;

        #region constructors

        public Database(IDatabaseDefinitionProvider dbDefinitionProvider, IStorage storage)
        {
            this._dbDefinitionProvider = dbDefinitionProvider;
            this._storage = storage;
        }

        public Database(IDatabaseDefinitionProvider dbDefinitionProvider, IStorage storage, DataContext.IDataContext dataContext) : this(dbDefinitionProvider, storage)
        {
            this._dataContext = dataContext;
            _userID = Guid.Empty;
        }

        public Database(IDatabaseDefinitionProvider dbDefinitionProvider, IStorage storage, DataContext.IDataContext dataContext, Guid userID) : this(dbDefinitionProvider, storage, dataContext)
        {
            this._userID = userID;
        }

        public Database(IDatabaseDefinitionProvider dbDefinitionProvider, IStorage storage, DataContext.IDataContext dataContext, Cache.ICacheProvider cacheProvider) : this(dbDefinitionProvider, storage, dataContext)
        {
            this._cacheProvider = cacheProvider;
        }

        public Database(IDatabaseDefinitionProvider dbDefinitionProvider, IStorage storage, DataContext.IDataContext dataContext, Cache.ICacheProvider cacheProvider, Guid userID) : this(dbDefinitionProvider, storage, dataContext, userID)
        {
            this._cacheProvider = cacheProvider;
        }

        #endregion

        #region properties

        public ContentFolder Content
        {
            get {
                if (this._content == null)
                {
                    this._content = new ContentFolder(this, this._dbDefinitionProvider, this._dataContext, this._cacheProvider);
                }
                return this._content;
            }
        }

        public FilesTree.FilesFolder Files
        {
            get {
                if (this._files == null)
                {
                    this._files = new FilesTree.FilesFolder();
                }
                return this._files;
            }
        }

        public IEnumerable<Security.IGroup> Groups
        {
            get { throw new NotImplementedException(); }
        }

        public IEnumerable<Security.IUser> Users
        {
            get { throw new NotImplementedException(); }
        }

        public IEnumerable<Links.ILink> Links
        {
            get { throw new NotImplementedException(); }
        }
        #endregion

        #region methods

        public INode GetNode(string path)
        {
            INode node = null;
            if (this._cacheProvider != null && this._cacheProvider.TryGetNodeFromCache(path, out node))
            {
                return node;
            }
            //return this._storage.GetNode(
            return null;
        }

        public INode GetNode(Guid ID)
        {
            return null;
        }


        #endregion

        #region helper methods


        #endregion
    }
}
